// pages/rule/rule.js
Page({
    /**
     * 页面的初始数据
     */
    data: {
        isGuanZhu:false,    //是否关注
        userMessage:{},   
        id: '',
        statusClass: ['is-complete', 'complete'],
        statusText: ['立即完善', '已完成'],
        rule: [
            { btnText: '立即绑定', num: 100, rule: '微信绑定会员', status: 0, key: 'register', },
            { btnText: '立即完善', num: 100, rule: '完善个人信息', status: 0, key: 'perfect_info', },
            { btnText: '立即添加', num: 100, rule: '首次添加美容顾问', status: 0, key: 'adviser' },
            { btnText: '立即测试', num: 100, rule: '首次肤质测试', status: 0, key: 'finish_question' },
            { btnText: '立即关注', num: 100, rule: '关注公众号', status: 0, key: 'follow_wechart' },
            // { btnText: '立即绑定', num: 100, rule: '淘宝绑定会员', status: 0, key: 'bind_taobao' },
            // { btnText: '立即完善', num: 100, rule: '完善淘宝会员信息', status: 0, key: 'perfect_taobao_info' },
        ],
        rule2: [
            { btnText: '立即邀请', num: 100, rule: '邀请好友注册', status: 1, key: 'friend_register' },
            { btnText: '', num: 100, rule: '好友完成测试', status: 1, key: 'friend_finish' },
            { btnText: '', num: 100, rule: '好友首次下单', status: 1, key: 'frend_purchase' }
        ]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var uinfo  = wx.getStorageSync('uinfo')
        if(uinfo&&uinfo.user){
            this.setData({
                id: uinfo.user.id,
                userMessage:uinfo.user
            })
            if(uinfo.user.id){
                this.getReluList()
            }
            if(uinfo.user.is_enterprise==0){
                this.getAsk()
            }
        }
    },
    getAsk(){  //查询是否绑定微信
        let that = this
        wx.$api.getUserEnterprise({
                user_id: that.data.id
            }).then(res=>{
                if(res.code>0){
                    if(res.data>0){
                        that.setData({
                            isGuanZhu:true,
                        })
                    }
                }else{
                    wx.showToast({
                      title: res.msg,
                      icon:'none'
                    })
                }
        })
    },
    
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },
    
    getReluList () {
        let that = this
        wx.$api.integralConfig({
                user_id: that.data.id
            }).then(res=>{
                let arr = res.data
                arr.forEach(e => {
                    for (let i in that.data.rule) {
                        if (that.data.rule[i].key == Object.keys(e)[0]) {
                            that.data.rule[i].num = e[that.data.rule[i].key]
                            that.data.rule[i].status = e.status ? 1 : 0
                        }
                    }
                    if(that.data.userMessage.subscribe==1){
                        that.data.rule[4].status = 1
                    }
                    for (let i in that.data.rule2) {
                        if (that.data.rule2[i].key == Object.keys(e)[0]) {
                            that.data.rule2[i].num = e[that.data.rule2[i].key]
                            that.data.rule2[i].status = e.status ? 1 : 0
                        }
                    }
                })
                //   let data   = {...res.data.data}
                //     for(let i in that.data.rule){
                //         console.log(11)
                //         that.data.rule[i].num =  data[that.data.rule[i].key]
                //         console.log( data[that.data.rule[i].key] )
                //     }
                //     for(let i in that.data.rule2){
                //         that.data.rule2[i].num =  data[that.data.rule2[i].key]
                //     }
                that.setData({
                    rule: that.data.rule,
                    rule2: that.data.rule2
                })
        })
    },
    handleClickRouter () {
        wx.navigateTo({
            url: "/pages/interests-rule/interests-rule"
        })
    },
    goRule1(e){
        let uinfo = this.data.userMessage
        let index = e.currentTarget.dataset.index
        let status = e.currentTarget.dataset.status
        if(status==1) return
        let url = ''
        if(index == 2&&that.data.isGuanZhu==true){
            console.log('123');
            return
        }else if(index==4&&this.data.rule[4].status<1){
            url = '/pages/gongzhonghao/gongzhonghao'
        }else if(!this.data.id||!this.data.rule[0].status){
            url = '/pages/register/register'
        }else if(index == 0){
            url = '/pages/register/register'
        }else if(index == 1){
            url = '/pages/info/info'
        }else if(index == 2){
            url = '/pages/servers-card/servers-card'
        }else if(index == 3){
            if (!uinfo || uinfo.is_member<1) {
                wx.setStorage({
                  data: '/pages/questionnaire/questionnaire',
                  key: 'lastUrl',
                })
                url = '/pages/register/register'
            }else{
                url = '/pages/questionnaire/questionnaire'
            }
        }else if(index == 5){
            return
            url = '/pages/register/register'
        }else if(index == 6){
            return
            url = '/pages/register/register'
        }
        wx.navigateTo({
          url: url,
        })
    },
    goRule2(e){
        wx.navigateTo({
            url: '/pages/sharePage/sharePage',
          })
        // let index = e.currentTarget.dataset.index
        // let status = e.currentTarget.dataset.status
    },
})